'''
建立开放代理的代理池
'''
import requests
from lxml import etree
class ProxyPool:
    def __init__(self):
        self.api_url = 'https://www.kuaidaili.com/free/inha/2/'
        self.test_url = "http://httpbin.org/get"#测试网站
        self.headers = {
            "User-Agent":'''ozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36'''
        }

    def get_proxy(self):
        html = requests.get(url=self.api_url,headers = self.headers).text
        eobj = etree.HTML(html)
        tr_list = eobj.xpath("//tr")#把所有tr拿到
        tr_list = tr_list[1:len(tr_list)]#去标题
        for tr in tr_list:
            ip1 = tr.xpath('.//td[@data-title="IP"]/text()')[0].strip()
            port1 = tr.xpath('.//td[@data-title="PORT"]/text()')[0].strip()
            proxy = ip1+":"+port1
            self.test_proxy(proxy)

    def test_proxy(self,proxy):
            '''测试从快代理中获取的ip和port是否可用，可用则存入代理池'''
            proxies ={
                "http":'http://{}'.format(proxy),
                "https":'https://{}'.format(proxy)
            }
            try:
                resp = requests.get(url=self.test_url,headers=self.headers,proxies = proxies,timeout=3)
                if resp.status_code ==200:
                    print(proxies,"可用")
                else:
                    print(proxies,"不可用")
            except :
                print(proxies, "不可用")


if __name__ == '__main__':
    spider = ProxyPool()
    spider.get_proxy()
